"use strict";
/* Controllers */
app.controller("rtMetricSyncConfigMngController", ['$scope', '$location', '$rootScope', '$q','$routeParams', 'rtMetricSyncConfigMngService', function ($scope, $location, $rootScope, $q, $routeParams, rtMetricSyncConfigMngService) {

    var vm = $scope.vm = {};
    vm.p = {};
    var routeId=$routeParams.id;

    $scope.getRtMetricSyncConfig = function () {
        var vm = $scope.vm;
        var params = {
            id: routeId
        };
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data
                vm.p.permissionType=vm.p.permissionType+""
            } else {
                alert(res.msg);
            }
        };
        return rtMetricSyncConfigMngService.getRtMetricSyncConfig(params, procFn, errorFn);
    };

    if(typeof(routeId)!="undefined"&&routeId!=null){
        $scope.getRtMetricSyncConfig()
    }

//    $('#dimensions').on('itemAdded', function(event) {
//        if(typeof(event.options)!='undefined'&&event.options.preventPost){
//            return;
//        }
//        var arr=$("#dimensions").tagsinput('items')
//        var resArr = getNewObjectOf(arr.sort());
//        $('#dimensions').tagsinput('removeAll');
//        for(var i in resArr){
//          $('#dimensions').tagsinput('add', resArr[i],{preventPost: true});
//        }
//    });
//
//
//    $('#indicators').on('itemAdded', function(event) {
//        var tag = event.item;
//        var  objRegExp= /^\w+_\w+$/;
//        if (!event.options || !event.options.preventPost) {
//            var res=objRegExp.test(tag);
//            if(!res){
//                $('#indicators').tagsinput('remove', tag, {preventPost: true});
//                alert("输入的内容不符合wtable的特征规范，请重新填写")
//            }
//        }
//
//    });

    function getNewObjectOf(src) {
      return Object.assign({}, src);
    }

    //e_time\s*[\)]?\s*[>|>=|<|<=|=]{1,2}\s*['|"]{1}%s['|"]{1}
    var errorFn = function (result) {
        alert(result);
    };

    $scope.rtMetricSyncConfigSave = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("保存成功！")
            } else {
                alert(response.msg);
            }
        };
        return rtMetricSyncConfigMngService.rtMetricSyncConfigSave(params, procFn, errorFn);
    };


}]);


/* Services */
app.service('rtMetricSyncConfigMngService', ['ajaxService', function (ajaxService) {

    this.rtMetricSyncConfigSave = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/rt_metric_config/set.do", successFunction, errorFunction);
    };

    this.getRtMetricSyncConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/rt_metric_config/detail.do", successFunction, errorFunction);
    };

}]);

